package com.aube.interact.model;

import com.aube.rvi.util.StringUtils;

import org.json.JSONObject;

import java.io.Serializable;

/**
 * Created by huyaonan on 16/3/23.
 */
public class GoodsItem implements Serializable {

    public String tlTitle;
    public String goodsURL;
    public String picurl;
    public String tlDesc;
    public String type;
    public String price;
    public String _id;
    public String showid;
    public String goodstype="";
    public String goodstypedesc="";

//    public String getPrice() {
//        if(StringUtils.isBlank(price))
//            return "";
//        if(price.length() == 1)
//            return price;
//        char c = price.charAt(0);
//        if(c >= 49 && c <= 57)
//            return price;
//        else
//            return price.substring(1);
//    }

    public int getPriceIndex() {
        if(StringUtils.isBlank(price))
            return -1;
        int i=0;
        while(i<price.length()) {
            char c = price.charAt(i);
            if (c >= 49 && c <= 57)
                return i;
            i++;
        }
        return -1;
    }

    public static GoodsItem parse(JSONObject jsonObject) {
        if(jsonObject == null)
            return new GoodsItem();
        GoodsItem item = new GoodsItem();
        item.tlTitle = jsonObject.optString("tlTitle");
        item.goodsURL = jsonObject.optString("goodsURL");
        item.picurl = jsonObject.optString("picurl");
        item.tlDesc = jsonObject.optString("tlDesc");
        item.type = jsonObject.optString("type");
        item.price = jsonObject.optString("price");
        item._id = jsonObject.optString("_id");
        item.showid = jsonObject.optString("showid");
        item.goodstype = jsonObject.optString("goodstype");
        item.goodstypedesc = jsonObject.optString("goodstypedesc");
        return item;
    }

    public String parsePrice(int index) {
        return "<font><small>" + price.substring(0, index) + "</small></font>" + price.substring(index);
    }

}
